﻿namespace com.vs.General
{
    using System;
    using System.Collections;
    using System.Text;
    using System.Threading;

    public class LockManager
    {
        private static Guid _inventoryGuid = Guid.NewGuid();
        private static Guid _launchSummaryGuid = Guid.NewGuid();
        private static Guid _loadNumGuid = Guid.NewGuid();
        private static Hashtable _locks = new Hashtable();
        private static Guid _shipmentDetailGuid = Guid.NewGuid();
        private static Guid _shipmentHeaderGuid = Guid.NewGuid();
        private static Guid _workUnitNameGuid = Guid.NewGuid();

        public static string CreateLockName(params string[] values)
        {
            StringBuilder builder = new StringBuilder("SCVLOCK");
            foreach (string str in values)
            {
                builder.Append("\x001e").Append(str);
            }
            return builder.ToString();
        }

        public static void GetInventoryLock(Session session, string item, string company, string warehouse)
        {
            GetLock(new string[] { _inventoryGuid.ToString(), item, company, warehouse, session.Environment });
        }

        public static void GetLaunchSummaryLock(Session session, string time, string warehouse)
        {
            GetLock(new string[] { _launchSummaryGuid.ToString(), time, warehouse, session.Environment });
        }

        public static void GetLock(params string[] values)
        {
            DateTime now = DateTime.Now;
            Monitor.Enter(GetLockObject(CreateLockName(values)));
        }

        public static object GetLockObject(string lockName)
        {
            object obj2;
            lock (_locks)
            {
                obj2 = _locks[lockName];
                if (obj2 == null)
                {
                    obj2 = new object();
                    _locks[lockName] = obj2;
                }
            }
            return obj2;
        }

        public static void GetShipLoadNumLock(Session session, string internalLoadNum)
        {
            GetLock(new string[] { _loadNumGuid.ToString(), internalLoadNum, session.Environment });
        }

        public static void GetShipmentDetailLock(Session session, string internalShipLineNum)
        {
            GetLock(new string[] { _shipmentDetailGuid.ToString(), internalShipLineNum, session.Environment });
        }

        public static void GetShipmentHeaderLock(Session session, string internalShipNum)
        {
            GetLock(new string[] { _shipmentHeaderGuid.ToString(), internalShipNum, session.Environment });
        }

        public static void GetWorkUnitBaseNameLock(Session session, string workUnitBaseName)
        {
            GetLock(new string[] { _workUnitNameGuid.ToString(), workUnitBaseName, session.Environment });
        }

        public static void ReleaseInventoryLock(Session session, string item, string company, string warehouse)
        {
            ReleaseLock(new string[] { _inventoryGuid.ToString(), item, company, warehouse, session.Environment });
        }

        public static void ReleaseLaunchSummaryLock(Session session, string time, string warehouse)
        {
            ReleaseLock(new string[] { _launchSummaryGuid.ToString(), time, warehouse, session.Environment });
        }

        public static void ReleaseLock(params string[] values)
        {
            Monitor.Exit(GetLockObject(CreateLockName(values)));
        }

        public static void ReleaseShipLoadNumLock(Session session, string internalLoadNum)
        {
            ReleaseLock(new string[] { _loadNumGuid.ToString(), internalLoadNum, session.Environment });
        }

        public static void ReleaseShipmentDetailLock(Session session, string internalShipLineNum)
        {
            ReleaseLock(new string[] { _shipmentDetailGuid.ToString(), internalShipLineNum, session.Environment });
        }

        public static void ReleaseShipmentHeaderLock(Session session, string internalShipNum)
        {
            ReleaseLock(new string[] { _shipmentHeaderGuid.ToString(), internalShipNum, session.Environment });
        }

        public static void ReleaseWorkUnitBaseNameLock(Session session, string workUnitBaseName)
        {
            ReleaseLock(new string[] { _workUnitNameGuid.ToString(), workUnitBaseName, session.Environment });
        }

        private static bool TryLock(params string[] values)
        {
            return Monitor.TryEnter(GetLockObject(CreateLockName(values)));
        }
    }
}

